package com.lhd_gm.scheduledtasksystem.job;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lhd_gm.scheduledtasksystem.config.SpringContext;
import com.lhd_gm.scheduledtasksystem.pojo.ScheduledHttpTask;
import com.lhd_gm.scheduledtasksystem.pojo.UniversalEntity;
import java.util.Iterator;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;

public class HttpRequestJob implements Job {
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String shtJson = jobDataMap.getString("httpConfig");
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            ScheduledHttpTask scheduledHttpTask = (ScheduledHttpTask)objectMapper.readValue(shtJson, ScheduledHttpTask.class);
            RestTemplate restTemplate = (RestTemplate)SpringContext.getBean(RestTemplate.class);
            HttpHeaders headers = new HttpHeaders();
            if (scheduledHttpTask.getHttpHeaders() != null) {
                Iterator var8 = scheduledHttpTask.getHttpHeaders().iterator();

                while(var8.hasNext()) {
                    UniversalEntity universalEntity = (UniversalEntity)var8.next();
                    headers.set(universalEntity.getKey(), universalEntity.getValue());
                }
            }

            HttpEntity<String> requestEntity = new HttpEntity(scheduledHttpTask.getHttpBody(), headers);
            restTemplate.exchange(scheduledHttpTask.getHttpUrl(), HttpMethod.valueOf(scheduledHttpTask.getHttpMethod()), requestEntity, String.class, new Object[0]);
        } catch (Exception var10) {
            Exception e = var10;
            throw new JobExecutionException(e);
        }
    }
}
